import { response } from "express";
import { request } from "express";
import CODE from "../../config/data.config.js";
import PetService from "../../service/petService/petService.js";
import * as service from '../../service/service.js'

class PetController {
    /**
     * @param {request} req 
     * @param {response} res 
     */
    static async GetOnlyOwner(req, res, next) {
        try {
            const { ownerID } = req.query;
            const result = await PetService.GetOnlyOwner(ownerID);
            res.send({
                code: 1,
                msg: '查询成功',
                data: result
            })
        } catch (error) {
            next(error)
        }
    }
    /**
     * @param {request} req
     * @param {response} res
     */
    static async InsertPet(req, res, next) {
        try {
            const condition = req.body;
            const status = await PetService.InsertPet(condition)
            if (status && status.affectedRows) {
                res.send({
                    msg: '添加成功',
                    code: 1
                })
            } else next({ msg: '添加失败', code: CODE.CODE_FAIL })
        } catch (error) {
            next(error)
        }
    }
    /**
     * @param {request} req
     * @param {response} res
     */
    static async InsertPetList(req, res, next) {
        try {
            const modelArray = req.body;
            const status = await PetService.InsertPetList(modelArray)
            if (status) {
                res.send({
                    msg: '添加成功',
                    code: 1,
                    data: status
                })
            } else next({ msg: '添加失败' })
        } catch (error) {
            next({ error })
        }
    }

    /**
     * @param {response} res
     * @param {request} req
     */
    static async DeletePet(req, res, next) {
        try {
            let { model } = req.body
            let state = await service.PetService.DeletePet(model)
            if (state)
                res.send({
                    msg: '删除成功',
                    code: CODE.CODE_OK,
                    data: state
                })
            else res.send({
                msg: '删除失败',
                code: CODE.CODE_FAIL,
                data: state
            })
        } catch (error) {
            next(error)
        }
    }
}



export default PetController;